﻿using Common.ConfigurationOptions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Domain.Context
{
    public class MySqlDbContext : ApplicationDBContext
    {
        public MySqlDbContext(IConfiguration configuration) : base(configuration)
        {
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            var position = "Dbs:ApplicationDB";
            var dbOptions = Configuration.GetSection(position)
                                         .Get<DbConfigOption>();
            if (string.IsNullOrWhiteSpace(dbOptions.Connection))
            {
                throw new ArgumentNullException(nameof(dbOptions.Connection));
            }
            var migrationAssembly = Assembly.GetExecutingAssembly().GetName().Name;
            var serverVersion = new MySqlServerVersion(new Version(8, 0, 29));
            optionsBuilder.UseMySql(dbOptions.Connection, serverVersion, action =>
            {
                action.MigrationsHistoryTable("MigrationHistory");
                action.MigrationsAssembly(migrationAssembly);
            });
            base.OnConfiguring(optionsBuilder);
        }
    }
}
